package com.yunfei.config.core.interceptor;

import com.yunfei.contants.CommonConsts;
import com.yunfei.contants.GlobMessageKeys;
import com.yunfei.service.RedisService;
import com.yunfei.utils.string.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 拦截器
 * @author:@zhengyunfei
 * @Date:2017-12-10 22:40
 */
@Service
public class RoleInterceptor implements HandlerInterceptor {
    @Autowired
    private RedisService redisService;
    @Autowired
    private Environment env;
    /**
     * 在请求处理之前进行调用（Controller方法调用之前）
     *
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        // 业务处理

        if(!StringUtil.isNullOrEmpty(env.getProperty(GlobMessageKeys.MAXEXPIRED))){
           int maxexpired= Integer.parseInt(env.getProperty(GlobMessageKeys.MAXEXPIRED));
           if(maxexpired<0){
               return CommonConsts.FALSEBOOL;
           }

        }
        Object bootstrap= Thread.currentThread().getContextClassLoader().getResource(GlobMessageKeys.BOOTSTRAP+GlobMessageKeys.PROPERTIES);
        if(StringUtil.isNullOrEmpty(bootstrap)){
            return CommonConsts.FALSEBOOL;
        }
        return CommonConsts.TRUEBOOL;

    }

    /**
     * 请求处理之后进行调用，但是在视图被渲染之前（Controller方法调用之后）
     *
     * @param httpServletRequest
     * @param httpServletResponse
     * @param o
     * @param modelAndView
     * @throws Exception
     */
    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
       // System.out.println("接口请求之前拦截。。。。。。。。。。。。。。。。。。。。。。。。。。。。");
    }

    /**
     * 在整个请求结束之后被调用，也就是在DispatcherServlet 渲染了对应的视图之后执行（主要是用于进行资源清理工作）
     *
     * @param httpServletRequest
     * @param httpServletResponse
     * @param o
     * @param e
     * @throws Exception
     */
    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
       // System.out.println("接口请求之后拦截。。。。。。。。。。。。。。。。。。。。。。。。。。。。");
    }

}
